#include<bits/stdc++.h>
using namespace std;
int rd(){
	int ans=0,f=1;char c=getchar();
	while(c<48||c>57){if(c=='-')f=-1;c=getchar();}
	while(c>47&&c<58)ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
	return ans*f;
}
#define f(a,b,c) for(int a=b;a<=c;++a)
#define F(a,b,c) for(int a=b;a>=c;--a)
#define int long long
int a[1515151],c[1515151],x[5];
string s[25];
int wi[1515151][21];// the person that wins the ith match in the jth round
int fiw[1515151],rmo[1515151];
int cli[1515151];
signed main(){
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	int n=rd(),m=rd();
	f(i,1,n*2)wi[i][0]=i;
	f(i,1,n)a[i]=rd();
	f(i,1,m)c[i]=rd();
	int k=0;
	while((1<<k)<n)++k;
	f(i,1,k)cin>>s[i],s[i]=" "+s[i];
	int T=rd();
	f(i,1,T)f(j,0,3)x[j]=rd();
	f(i,1,n)a[i]^=x[i%4];
	if(n<=50){
		int fin=0;
		f(i,1,m){
			int k2=0;
			while((1<<k2)<c[i])++k2;
			f(j,c[i]+1,(1<<k2)+1){
				// the ability of j is INF, while other 'virtual participants' have ability 0
				f(x,1,k2)f(y,1,(1<<(k2-x))){
					int t=s[x][y]-'0',rr=0;
					rr=wi[y*2-1+t][x-1];
					if((rr<=c[i] and a[rr]>=x) or rr==j)wi[y][x]=rr;
					else wi[y][x]=wi[y*2-t][x-1];
				}
				fiw[wi[1][k2]]=i;
			}
			int ans=0;
			f(j,1,(1<<k2))if(fiw[j]==i)ans+=j;
			fin^=(ans*i);
		}
		cout<<fin<<endl;
	}
	else{
		bool dfd=true;
		f(x,1,k)f(y,1,(1<<(k-x)))if(s[x][y]=='1'){
			dfd=false;break;
		}
		if(!dfd){
			f(x,1,k)f(y,1,(1<<(k-x))){
				int t=s[x][y]-'0',rr=0;
				rr=wi[y*2-1+t][x-1];
				if((rr<=n and a[rr]>=x))wi[y][x]=rr;
				else wi[y][x]=wi[y*2-t][x-1];
			}
			int ans=0;
			f(i,1,m){
				int k2=0;
				while((1<<k2)<c[i])++k2;
				ans^=(i*wi[1][k2]);
			}
			cout<<ans<<endl;
		}
		else{
			f(i,1,n){
				int p1=i;
				rmo[i]=i;
				f(j,1,k){
					bool plc=((i-1)&(1<<(j-1)));
					p1=(p1+1)/2;
					if(plc==0 and a[i]>=j)cli[i]=j,wi[p1][j]=i,rmo[i]=(1<<j)*p1;
					else if(plc==1 and !wi[p1][j])cli[i]=j,wi[p1][j]=i,rmo[i]=(1<<j)*p1;
					else break;
				}
			}
			f(i,1,n)rmo[i]=max(rmo[i],rmo[i-1]);
			int fin=0;
			f(i,1,m){
				int k2=0;
				while((1<<k2)<c[i])++k2;
				if(rmo[c[i]]>=(1<<k2))fin^=(i*wi[1][k2]);
				else{
					int L=rmo[c[i]]+1,R=(1<<k2);
					fin^=(i*((R+L)*(R-L+1)/2));
				}
			}
			cout<<fin<<endl;
		}
	}
	return 0;
}
/*
BpdrKRKmpRjKOf1o1qgpTI5IwjiKBgCNUnCoEWY2yHMKvL7wCnjd5ov0XdIjHS4lFGXSm3UiLo3EAA8KvrvzfqzARFrgivQv9xNuzpcIefWmnc6jTzg6pfEEs6sazIW9Er3DhfVJJz54b9l28R6v4K7xQzVOGzXUrafYD8rU5uZg3KgBmm4q6BlVAHtQGOs5yVd9bSegKBKNT0WFma4rlpKu4BILZBOVe2eGSGwnPH8tHd62D8rxwBr0M9JvIiQxi2B8H7tgM2XcdeeqmTlieah0k0t0gHvOJ4XYCODWOk81Oks9DbppCWpuXgsBxoaFs5n2T0Xsldr7MhEXJ4NVaAN5rFHo3p3uspuLpRBY33ePksw1wJWVUtYJ7p78c92UwvojKzIN0umjmhkhyFCS6kjDYpM8yMdsFRbaOrvNlg6WNoDL3NmA5VLbKfkG0L8Fmin8ZjVJNbpkQ03RLnZQHv0bYiqY3wmpeZvCGRVd2ILQGNsbAPzRIz1rhrNil9XOhRaxs3ZsJuJZH9AQY7rr7sgmh3VTD0rsRPpHQOAjHRGWaQw6Xovc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